package com.itheima.homework7;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Demo7 {
    public static void main(String[] args) {
        ArrayList<Double> list = new ArrayList<>();
        getScore(list);//调用方法获得评委成绩
        double max = list.get(0);
        double min = list.get(0);
        double sum = 0;
        //获得max，min，以及sum
        for (int i = 0; i < list.size(); i++) {
            max = getMax(list, max, i);//调用方法获取最大值
            min = getMin(list, min, i);//调用方法获取最小值
            sum += list.get(i);//调用方法求和
        }
        double result = getAverage(list, max, min, sum);
        System.out.println("选手最终得分为：" + result);
    }

    //平均分
    private static double getAverage(ArrayList<Double> list, double max, double min, double sum) {
        return (sum - max - min) / (list.size() - 2);
    }

    //最小值
    private static double getMin(ArrayList<Double> list, double min, int i) {
        if (list.get(i) < min) {
            min = list.get(i);
        }
        return min;
    }

    //最大值
    private static double getMax(ArrayList<Double> list, double max, int i) {
        if (list.get(i) > max) {
            max = list.get(i);
        }
        return max;
    }

    //获得评委成绩
    private static void getScore(ArrayList<Double> list) {
        for (int i = 1; i < 9; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入第" + i + "个评委成绩");
            double v = sc.nextDouble();
           if(v<0||v>100){
               System.out.println("录入的成绩有误，请重新录入");
               i--;
           }else {
               list.add(v);
           }
        }
    }
}
